/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TPointDetail;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TPointDetailService;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BaseDialog;

/**
 * 说明：
 * 会员详细Dialog
 * @Author：李智深
 * @Date：2012-03-20
 */
public class MemberPointSend extends BaseDialog{

	private static final long serialVersionUID = 1L;
	
	private JButton okButton;//确定
	private JButton cancelButton;//关闭
//	private JLabelJTextFieldComponent menberNumberTF=null;//会员编号
//	private JLabelJTextFieldComponent menberCardId;//会员卡号
//	private JLabelJTextFieldComponent menberUserNameTF=null;//用户名
//	private JLabelJTextFieldComponent menberRealName=null;//姓名
//	private JLabelJTextFieldComponent menberMobileTF=null;//手机号
//	private JLabelJTextFieldComponent menberBuyCount=null;//消费次数
//	private JLabelJTextFieldComponent menberPoint=null;//用户积分
//	private JLabelJTextFieldComponent menberStatusTF=null;//状态
//	private JLabelJTextAreaComponent menberLocation=null;//详细地址
//	private JLabelJTextFieldComponent menberCreateTime=null;//注册时间
	private JLabelJTextFieldComponent memberPointSend=null;//赠送积分
	private String userID;//会员编号
	private String locationID;//会员编号
	private TUserService tUserService;//用户操作 
	private TPointDetailService tPointService;//用户操作
	List<TUser> tUser = new ArrayList();
	public MemberPointSend(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		tPointService=BuildSpringApplication.getInstance().getTPointDetailImpl();
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	
	private void initViews(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
//		menberNumberTF=new JLabelJTextFieldComponent("会员编号：",30);
//		menberCardId = new JLabelJTextFieldComponent("会员卡号：", 30);
//		menberUserNameTF=new JLabelJTextFieldComponent("用 户 名：",30);
//		menberRealName=new JLabelJTextFieldComponent("姓    名：",30);
//		menberMobileTF=new JLabelJTextFieldComponent("手 机 号：",30);
//		menberBuyCount=new JLabelJTextFieldComponent("消费次数：",30);
//		menberPoint = new JLabelJTextFieldComponent("用户积分",30);
//		menberStatusTF=new JLabelJTextFieldComponent("状    态：",30);
//		menberLocation=new JLabelJTextAreaComponent("详细地址：", 23, 2);
//		menberCreateTime=new JLabelJTextFieldComponent("注册时间：",30);
		memberPointSend = new JLabelJTextFieldComponent("赠送积分:",30);
//		menberNumberTF.getText().setEditable(false);
//		menberPoint.getText().setEditable(false);
//		menberCreateTime.getText().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("确定");
		cancelButton = new RPJButtonGray("关闭");
		jp1.add(okButton);
		jp1.add(cancelButton);
		jp1.setOpaque(false);
		
//		main_jp.add(menberNumberTF);
//		main_jp.add(menberCardId);
//		main_jp.add(menberUserNameTF);
//		main_jp.add(menberRealName);
//		main_jp.add(menberMobileTF);
//		main_jp.add(menberBuyCount);
//		main_jp.add(menberPoint);
//		main_jp.add(menberStatusTF);
//		main_jp.add(menberLocation);
//		main_jp.add(menberCreateTime);
		main_jp.add(memberPointSend);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		if(e.getSource()==okButton){
			Boolean isShow =false;
			//确定
			String pointSend=memberPointSend.getText().getText().trim(); //赠送积分
			if(!MatcherFormatUtil.MatcherNumberScheme(pointSend)){
				JOptionPane.showMessageDialog(null, "赠送积分 不能为空或格式不正确。");
				memberPointSend.getText().setText("");
				memberPointSend.getText().requestFocus();
				return;
			}
			//TUser tUser=new TUser();
			//tUser.setUserID(menberNumberTF.getText().getText().trim());
			//tUser.setCardNum(menberCardId.getText().getText().trim());
			for(int i = 0; i < tUser.size(); i++){
				int point= tUser.get(i).getPoint() + Integer.parseInt(pointSend);
				tUser.get(i).setPoint(point);
				try {
					if(tUserService.modifyRechargeService(tUser.get(i))){
						BuildSpringApplication.getInstance().getTUserServiceImpl().saveOrUpdateService(tUser.get(i));
						//tUserService.saveOrUpdateService(tUser);
						TPointDetail tPointDetail = new TPointDetail();//添加明细
						tPointDetail.setPointDetailID(UUID.GenUUID15());
						tPointDetail.setType(7);
						tPointDetail.setUserID(tUser.get(i).getUserID());
						tPointDetail.setHappenTime(date);
						tPointDetail.setLocationID(locationID);
						tPointDetail.setPoint(Integer.parseInt(pointSend));
						tPointDetail.setRelItemID(tUser.get(i).getUserID());
						tPointService.saveService(tPointDetail);
						isShow=true;
//						//添加资金出入
//						ApplicationContext.addMondyInOut(ApplicationContext.MONEYINOUT_TYPE_IN, 
//								ApplicationContext.OPERATOR_ID, ApplicationContext.MONEYINOUT_SOURCE_TYPE5, tBillDetail.getDetailID());
//						
						//JOptionPane.showMessageDialog(null, "赠送成功");
						
					}else{
						//JOptionPane.showMessageDialog(null, "赠送失败");
					}
				} catch (Exception e1) {
					isShow=false;
					e1.printStackTrace();
				}
			}
			if(isShow){
				JOptionPane.showMessageDialog(null, "积分赠送成功!");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(null, "数据异常。");
			}

			
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	
	public void showDialog(List<TUser> user){
//		if(user!=null){
//			tuser=user;
//			userID=user.getUserID();
//			locationID=user.getLocation();
//			menberNumberTF.getText().setText(user.getUserID());
//			menberCardId.getText().setText(user.getCardNum());
//			menberUserNameTF.getText().setText(user.getUserName());
//			menberRealName.getText().setText(user.getRealName());
//			menberMobileTF.getText().setText(user.getMobile());
//			menberBuyCount.getText().setText(user.getBuyCount()+"");
//			menberLocation.getArea().setText(user.getLocation());
//			menberPoint.getText().setText(user.getPoint()+"");
//			if(user.getStatus()==1){
//				menberStatusTF.getText().setText("正常");
//			}else{
//				menberStatusTF.getText().setText("锁定");
//			}
//			menberCreateTime.getText().setText(user.getCreateTime().toString());
//		}
//		this.setVisible(true);
		
		for(int i =0;i<user.size();i++){
			tUser.add(user.get(i));
		}
		//tUser=user;
		this.setVisible(true);
	}
}
